package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Exam;
import com.woniuxy.dao.ExamDao;
import com.woniuxy.service.ExamService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Exam)表服务实现类
 *
 * @author zjx
 * @since 2021-05-14 19:41:32
 */
@Service("examService")
public class ExamServiceImpl implements ExamService {
    @Resource
    private ExamDao examDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Exam queryById(Integer id) {
        return this.examDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Exam> queryAllByLimit(int offset, int limit) {
        return this.examDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param exam 实例对象
     * @return 实例对象
     */
    @Override
    public Exam insert(Exam exam) {
        this.examDao.insert(exam);
        return exam;
    }

    /**
     * 修改数据
     *
     * @param exam 实例对象
     * @return 实例对象
     */
    @Override
    public Exam update(Exam exam) {
        this.examDao.update(exam);
        return this.queryById(exam.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.examDao.deleteById(id) > 0;
    }

    @Override
    public PageInfo<Map<String,String>> queryAll(Integer startPage, Integer pageSize, Exam exam) {
        PageHelper.startPage(startPage,pageSize);
        Page<Map<String,String>> page = (Page<Map<String,String>>) this.examDao.queryAll(exam);
        PageInfo<Map<String,String>> examPageInfo = new PageInfo<>(page);
        return examPageInfo;
    }


    @Override
    public PageInfo<Map<String, String>> queryAllStudents(Map<String, Object> map) {
       int startPage = (int) map.get("startPage");
       int pageSize = (int) map.get("pageSize");
       PageHelper.startPage(startPage,pageSize);
       //获取前端含有考试id 学员信息 考试状态的对象
        Map<String,String> examDetail= (Map<String, String>) map.get("examDetail");
        //获取对应
        PageInfo<Map<String,String>> pageInfo = new PageInfo<>(this.examDao.queryAllStudents(examDetail)) ;
        return pageInfo;
    }

    @Override
    public PageInfo<Map<String, String>> queryMakeUpAll(Integer startPage,Integer pageSize,Integer id) {
        PageHelper.startPage(startPage,pageSize);
        PageInfo<Map<String,String>> pageInfo = (PageInfo<Map<String, String>>) this.examDao.queryMakeUpAll(id);
        return pageInfo;
    }

    @Override
    public PageInfo<Map<String, String>> queryMakeUpStudent(Integer startPage, Integer pageSize, Integer id,String studentName) {
        PageHelper.startPage(startPage,pageSize);
        PageInfo<Map<String,String>> pageInfo = (PageInfo<Map<String, String>>) this.examDao.queryMakeUpStudent(id,studentName);
        return pageInfo;
    }

    @Override
    public PageInfo<Map<String, String>> queryReview(Integer startPage, Integer pageSize, Integer id, String studentName, Integer reviewStatus) {
        PageHelper.startPage(startPage,pageSize);
        PageInfo<Map<String,String>> pageInfo = (PageInfo<Map<String, String>>) this.examDao.queryReview(id,studentName,reviewStatus);
        return pageInfo;
    }
}
